package com.zmht.beike.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.StudentTwo;
import com.zmht.beike.po.TCognition;
import com.zmht.beike.util.TimeUtil;
import com.zmht.beike.web.form.CognitionForm;
import com.zmht.specedu.vo.Teacher;

public class CognitionAction extends BaseAction {
	
	public ActionForward getCognition(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String studentId = request.getParameter("studentId");
		TCognition tc = null;
		if("edit".equals(request.getSession().getAttribute("action"))){
			tc = this.getCognitionBiz().findByStudentId(studentId);
		}
		request.setAttribute("cognition", tc);
		request.setAttribute("studentId",studentId);
		return new ActionForward("/jsp/pingce/ceping_cognition.jsp");
	}
	/**
	 * 更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateCognition(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String studentId = request.getParameter("studentId");
		TCognition tc = new TCognition();
		CognitionForm frm = (CognitionForm)form;
		BeanUtils.copyProperties(tc, frm);
		tc.setCtime(TimeUtil.getYMDTime());
		
		if(new Integer(0).equals(frm.getId())){
			this.getCognitionBiz().saveTCognition(tc);
		}else{
			this.getCognitionBiz().updateTCognition(tc);
		}
		
		StudentTwo student = (StudentTwo)this.getStudentBiz().getStudentDAO().findById(StudentTwo.class, new Integer(studentId));
		student.setSumCognition(tc.getScore());
		student.setPingceDate(TimeUtil.getYMDTime());
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		student.setWreteName(teacher.getTname());
		this.getStudentBiz().getStudentDAO().updateObj(student);
		
		request.setAttribute("studentId", studentId);
		return new ActionForward("/beike/cognition.do?method=getCognition&studentId="+studentId);
	}
}
